package com.mytrip.webapp.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mytrip.base.action.StrutsActionAdapter;
import com.mytrip.model.City;
import com.mytrip.model.FlightCity;
import com.mytrip.util.Caches;

public class JsonAction extends StrutsActionAdapter{

	private static final long serialVersionUID = -3378752878708715479L;

	public String city(){
		String k = null;
		try {
			k = new String(getRequest().getParameter("k").getBytes("ISO8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		k = k.replaceAll("\\s+", " ");
		k = k.trim();

		JSONObject obj = new JSONObject();
		JSONArray arr = new JSONArray();
		for (City c : Caches.cityList) {
			if (c.getName().contains(k) || c.getProvince().contains(k) || c.getNamePy().contains(k.toLowerCase())
					|| c.getProvincePy().contains(k.toLowerCase())) {
				JSONObject o = new JSONObject();
				o.put("id", c.getId());
				o.put("value", c.getName());
				o.put("info", c.getProvince());
				arr.add(o);
			}
		}
		obj.put("results", arr);
		String msg = obj.toString();

		try {
			getResponse().getWriter().write(msg);
		} catch (IOException e) {
			log.error("", e);
		}

		// try {
		// getResponse().getWriter().write(msg);
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		return null;
	}

	public String flightCity(){
		String k = null;
		try {
			k = new String(getRequest().getParameter("k").getBytes("ISO8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		k = k.replaceAll("\\s+", " ");
		k = k.trim();

		JSONObject obj = new JSONObject();
		JSONArray arr = new JSONArray();
		for (FlightCity c : Caches.flightCityList) {
			if (c.getCityName().contains(k) || c.getAirportName().contains(k)
					|| (c.getCity3code() != null && c.getCity3code().contains(k.toUpperCase()))) {
				JSONObject o = new JSONObject();
				o.put("id", c.getCity3code());
				o.put("value", c.getAirportName());
				o.put("info", c.getCityName());
				arr.add(o);
			}
		}
		obj.put("results", arr);
		String msg = obj.toString();

		try {
			getResponse().getWriter().write(msg);
		} catch (IOException e) {
			log.error("", e);
		}

		return null;
	}

	public String hotelCity(){
		String k = null;
		try {
			k = new String(getRequest().getParameter("k").getBytes("ISO8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		k = k.replaceAll("\\s+", " ");
		k = k.trim();

		JSONObject obj = new JSONObject();
		JSONArray arr = new JSONArray();
		for (City c : Caches.cityList) {
			if (c.getTongchengCityId() != null
					&& (c.getName().contains(k) || c.getProvince().contains(k)
							|| c.getNamePy().contains(k.toLowerCase()) || c.getProvincePy().contains(k.toLowerCase()))) {
				JSONObject o = new JSONObject();
				o.put("id", c.getTongchengCityId());
				o.put("value", c.getName());
				o.put("info", c.getProvince());
				arr.add(o);
			}
		}
		obj.put("results", arr);
		String msg = obj.toString();

		try {
			getResponse().getWriter().write(msg);
		} catch (IOException e) {
			log.error("", e);
		}

		return null;
	}

}
